
const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    starScore: cc.Label = undefined
    @property([cc.Prefab])
    monsters: cc.Prefab[] = []
    @property(cc.Node)
    back: cc.Node = undefined
    @property(cc.Label)
    tip: cc.Label = undefined
    @property(cc.Node)
    leftBtn: cc.Node = undefined
    @property(cc.Node)
    rightBtn: cc.Node = undefined
    @property(cc.Node)
    monsterBody: cc.Node = undefined
    @property(cc.Node)
    chooseBtn: cc.Node = undefined

    private index: number = 0                               //当前选择的怪物
    private defaultChooseNum: number = 0                    //当前选中的怪物
    private price: number[] = [0, 15, 25, 35, 45, 50]       //价格
    private startNum: number = 0                            //默认开始的选择下标
    private boughtList: number[] = []                       //已经购买了的怪物

    onLoad() {
        this.initScene()
        this.initLoadMonster()
        this.addEventListen()
    }

    // 初始化
    private initScene() {
        this.initStart()
        this.initBoughtList()
    }

    // 初始化购买的怪物
    private initBoughtList() {
        let list = cc.sys.localStorage.getItem('bought')
        if (list) {
            this.boughtList = JSON.parse(list)
        } else {
            this.boughtList.push(0)
            cc.sys.localStorage.setItem('bought', JSON.stringify(this.boughtList))
        }
    }

    // 初始化界面
    private initStart() {
        let num = cc.sys.localStorage.getItem('star')
        if (num) {
            num = num < 0 ? 0 : num
            this.starScore.string = 'x ' + num + ''
            this.startNum = num
        } else {
            this.starScore.string = 'x ' + 0 + ''
        }
    }

    // 初始化加载怪物
    private initLoadMonster() {
        let node: cc.Node = undefined
        let monster = cc.sys.localStorage.getItem('monster')
        if (monster) {
            node = cc.instantiate(this.monsters[+monster])
            this.index = +monster
            this.defaultChooseNum = +monster
        } else {
            node = cc.instantiate(this.monsters[0])
            cc.sys.localStorage.setItem('monster', 0)
            this.index = 0
            this.defaultChooseNum = 0
        }
        this.tip.string = 'selected'
        node.parent = this.monsterBody
        if (this.index == 0) {
            this.leftBtn.opacity = 100
        }
        if (this.index == this.monsters.length - 1) {
            this.rightBtn.opacity = 100
        }

    }

    // 加载怪物
    private loadMonster() {
        this.monsterBody.removeAllChildren()
        let node: cc.Node = cc.instantiate(this.monsters[this.index])
        node.parent = this.monsterBody
        if (this.boughtList.includes(this.index)) {
            this.tip.string = 'bought'
        }
        else {
            this.tip.string = this.price[this.index] + ''
        }
        if (this.index == this.defaultChooseNum) {
            this.tip.string = 'selected'
        }
        this.leftBtn.opacity = 255
        this.rightBtn.opacity = 255
        if (this.index == 0) {
            this.leftBtn.opacity = 100
        }
        if (this.index == this.monsters.length - 1) {
            this.rightBtn.opacity = 100
        }
    }

    // 添加点击事件
    private addEventListen() {
        this.leftBtn.on('touchstart', this.leftEvent, this)
        this.rightBtn.on('touchstart', this.rightEvent, this)
        this.back.on('touchstart', this.backEvent, this)
        this.chooseBtn.on('touchstart', this.chooseEvent, this)
    }

    // 左移事件
    private leftEvent() {
        if (this.index == 0) return
        this.index -= 1
        this.loadMonster()
    }

    // 右移事件
    private rightEvent() {
        if (this.index == this.monsters.length - 1) return
        this.index += 1
        this.loadMonster()
    }

    // 返回事件
    private backEvent() {
        cc.director.loadScene('start')
    }

    // 选择怪物
    private chooseEvent() {
        if (this.startNum < this.price[this.index] && !this.boughtList.includes(this.index)) return
        if (!this.boughtList.includes(this.index)) {
            let n = this.startNum - this.price[this.index]
            cc.sys.localStorage.setItem('star', n)
            this.boughtList.push(this.index)
            cc.sys.localStorage.setItem('bought', JSON.stringify(this.boughtList))
            this.initStart()
        }
        cc.sys.localStorage.setItem('monster', this.index)
        this.defaultChooseNum = this.index
        this.loadMonster()
    }

    // update (dt) {}
}
